package com.googlecode.garbagecan.beanutils;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;

import java.beans.PropertyDescriptor;
import java.util.Calendar;

public class PropertyUtilsSample {
	public static void main(String[] args) throws Exception {
		//test1();
		test2();
	}

	public static void test1() throws Exception {
		SampleBean bean = new SampleBean();
		PropertyUtils.setProperty(bean, "strProperty", "str");
		PropertyUtils.setProperty(bean, "integerProperty", null);// Take care, will get null.
		PropertyUtils.setProperty(bean, "intProperty", 0);
		PropertyUtils.setProperty(bean, "longProperty", 1234567890);
		PropertyUtils.setProperty(bean, "dateProperty", Calendar.getInstance().getTime());

		System.out.println("String property: " + PropertyUtils.getProperty(bean, "strProperty"));
		System.out.println("Integer Property: " + PropertyUtils.getProperty(bean, "integerProperty"));
		System.out.println("int Property: " + PropertyUtils.getProperty(bean, "intProperty"));
		System.out.println("long Property: " + PropertyUtils.getProperty(bean, "longProperty"));
		System.out.println("Date Property: " + PropertyUtils.getProperty(bean, "dateProperty"));

		System.out.println("String property: " + bean.getStrProperty());
		System.out.println("Integer Property: " + bean.getIntegerProperty());
		System.out.println("int Property: " + bean.getIntProperty());
		System.out.println("long Property: " + bean.getLongProperty());
		System.out.println("Date Property: " + bean.getDateProperty());
	}

	public static void test2() throws Exception {
		SampleBean bean = new SampleBean();
		PropertyUtils.setProperty(bean, "strProperty", "str");
		PropertyUtils.setProperty(bean, "integerProperty", null);
		PropertyUtils.setProperty(bean, "intProperty", 0);
		PropertyUtils.setProperty(bean, "longProperty", 1234567890);
		PropertyUtils.setProperty(bean, "dateProperty", Calendar.getInstance().getTime());

		System.out.println("property type: " + PropertyUtils.getPropertyType(bean, "dateProperty"));

		PropertyDescriptor[] pds;
		System.out.println("\n\nGet property descriptor by bean");
		pds = PropertyUtils.getPropertyDescriptors(bean);
		for (int i = 0; i < pds.length; i++) {
			System.out.println("property name: " + pds[i].getName());
			System.out.println("property display name: " + pds[i].getDisplayName());
			System.out.println("property type: " + pds[i].getPropertyType());
			System.out.println();
		}

		System.out.println("\n\nGet property descriptor by class");
		pds = PropertyUtils.getPropertyDescriptors(SampleBean.class);
		for (int i = 0; i < pds.length; i++) {
			System.out.println("property name: " + pds[i].getName());
			System.out.println("property display name: " + pds[i].getDisplayName());
			System.out.println("property type: " + pds[i].getPropertyType());
			System.out.println();
		}
	}
}
